package gui;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;

import clientPackage.ObservableServer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
/**
 * 
 * @author Idan Mann
 * This GUI class presents a simple console showing the server IP
 * and a shutdown button
 *
 */
public class ServerGui extends JFrame
{
	private JTextField textFieldIP;
	private JButton btnShutdown;
	public ServerLog myLog;
	private JTextField textFieldPort;
	private JTextField txtFieldSQLServer;
	private JTextField textFieldSQLPassword;
	private JTextField textFieldUserName;
	private JButton btnConnect;
	public ServerGui() 
	{
		getContentPane().setLayout(null);
		myLog = new ServerLog();
		myLog.setSize(700, 500);
		myLog.setVisible(true);
		JLabel lblNewLabel = new JLabel("Server");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNewLabel.setBounds(179, 11, 205, 28);
		getContentPane().add(lblNewLabel);
		
		JLabel lblMyIp = new JLabel("My IP:");
		lblMyIp.setBounds(89, 53, 70, 14);
		getContentPane().add(lblMyIp);
		
		textFieldIP = new JTextField();
		textFieldIP.setEditable(false);
		textFieldIP.setBounds(179, 50, 168, 20);
		getContentPane().add(textFieldIP);
		textFieldIP.setColumns(10);
		
		this.textFieldIP.setText("");
		
		btnShutdown = new JButton("Shutdown");
		btnShutdown.setEnabled(false);
		btnShutdown.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try 
				{
					myLog.logFile.close();
				} 
				catch (IOException e1)
				{
					// TODO Auto-generated catch block
				//	e1.printStackTrace();
				}
				System.exit(0);
			}
		});
		btnShutdown.setBounds(160, 224, 128, 23);
		getContentPane().add(btnShutdown);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(89, 81, 46, 14);
		getContentPane().add(lblPort);
		
		textFieldPort = new JTextField();
		textFieldPort.setText("5555");
		textFieldPort.setBounds(179, 78, 168, 20);
		getContentPane().add(textFieldPort);
		textFieldPort.setColumns(10);
		
		JLabel lblSqlServer = new JLabel("SQL Server");
		lblSqlServer.setBounds(89, 109, 79, 14);
		getContentPane().add(lblSqlServer);
		
		txtFieldSQLServer = new JTextField();
		txtFieldSQLServer.setText("mysql://localhost/test");
		txtFieldSQLServer.setBounds(179, 106, 168, 20);
		getContentPane().add(txtFieldSQLServer);
		txtFieldSQLServer.setColumns(10);
		
		JLabel lblSqlPassword = new JLabel("SQL password");
		lblSqlPassword.setBounds(89, 162, 79, 14);
		getContentPane().add(lblSqlPassword);
		
		textFieldSQLPassword = new JTextField();
		textFieldSQLPassword.setBounds(179, 159, 168, 20);
		getContentPane().add(textFieldSQLPassword);
		textFieldSQLPassword.setColumns(10);
		
		btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				btnShutdown.setEnabled(true);
				btnConnect.setEnabled(false);
				
				ObservableServer sv = new ObservableServer(Integer.parseInt(textFieldPort.getText()), txtFieldSQLServer.getText(), textFieldUserName.getText() , textFieldSQLPassword.getText(), myLog );
			    
			    try 
			    {
			      sv.listen(); //Start listening for connections
			    } 
			    catch (Exception ex) 
			    {
			    
			    }
			}
		});
		btnConnect.setBounds(160, 190, 128, 23);
		getContentPane().add(btnConnect);
		
		JLabel lblSqlUsername = new JLabel("SQL Username");
		lblSqlUsername.setBounds(89, 137, 79, 14);
		getContentPane().add(lblSqlUsername);
		
		textFieldUserName = new JTextField();
		textFieldUserName.setText("root");
		textFieldUserName.setColumns(10);
		textFieldUserName.setBounds(179, 134, 168, 20);
		getContentPane().add(textFieldUserName);
		
		
		Socket s = null;
		try {
			s = new Socket("google.com", 80);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			(new Dialog("Could not connect to the internet. Shuting down.")).setVisible(true);
		}
		String ip = s.getLocalAddress().getHostAddress();
		//System.out.println("Ip of local host is: "+s.getLocalAddress().getHostAddress());
		this.textFieldIP.setText(ip);
		
		 try {
				s.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
			//	e.printStackTrace();
			}
	}
	
	
	// Fake server for tests
	public void fakeServer(int port, String sqlServer, String username , String password)
	{
		btnShutdown.setEnabled(true);
		btnConnect.setEnabled(false);
		
		ObservableServer sv = new ObservableServer(port,sqlServer,username,password, new ServerLog() );
	    
	    try 
	    {
	      sv.listen(); //Start listening for connections
	    } 
	    catch (Exception ex) 
	    {
	      //System.out.println("ERROR - Could not listen for clients!");
	    }
	}
}
